//
//  NSBundle+HYAPP.m
//  HYApp
//
//  Created by victor lau on 2018/12/18.
//  Copyright © 2018 Unidax. All rights reserved.
//

#import "NSBundle+HYAPP.h"
#import "HYMacro.h"
#import <JFBase/JFBase.h>

@implementation NSBundle (HYAPP)


+(NSString *)localizedStringForKey:(NSString *)key
{
    return [self localizedStringForKey:key value:nil];
}


+ (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value
{
    
    NSString *languageStr =  [[NSUserDefaults standardUserDefaults] objectForKey:AppLanguage];
    
    if ([JFStringHelper isEmpty:languageStr]) {
        // （iOS获取的语言字符串比较不稳定）目前框架只处理en、zh-Hans、zh-Hant三种情况，其他按照系统默认处理
        NSString *language = [NSLocale preferredLanguages].firstObject;
        if ([language hasPrefix:@"en"]) {
            language = @"en";
        } else if ([language hasPrefix:@"zh"]) {
            if ([language rangeOfString:@"Hans"].location != NSNotFound) {
                language = @"zh-Hans"; // 简体中文
            } else { // zh-Hant\zh-HK\zh-TW
                language = @"zh-Hant"; // 繁體中文
            }
        } else {
            language = @"en";
        }
        languageStr = language;
    }
    // 从MJRefresh.bundle中查找资源
    NSString *  path = [[NSBundle  mainBundle] pathForResource:languageStr ofType:@"lproj"];
   
    value =   [[NSBundle bundleWithPath:path] localizedStringForKey:key value:@"" table:nil];
    return value;
}


@end
